import axios from 'axios';

const instance = axios.create({
  baseURL: '' // 确保使用相对路径
});

instance.interceptors.request.use(config => {
  // 打印请求信息
  console.log('发送请求:', {
    url: config.url,
    method: config.method,
    baseURL: config.baseURL,
    data: config.data,
    headers: config.headers
  });
  // 正确从user-storage中获取token
  let token = '';
  const userStore = localStorage.getItem('user-storage');
  if (userStore) {
    try {
      token = JSON.parse(userStore).state.token;
    } catch (e) {}
  }
  if (token) {
    config.headers['token'] = token;
  }
  return config;
});

instance.interceptors.response.use(
  response => {
    // 打印响应信息
    console.log('请求成功:', {
      url: response.config.url,
      status: response.status,
      data: response.data
    });
    return response;
  },
  error => {
    // 打印详细错误信息
    console.error('请求错误:', {
      url: error.config?.url,
      method: error.config?.method,
      status: error.response?.status,
      statusText: error.response?.statusText,
      data: error.response?.data,
      message: error.message
    });
    return Promise.reject(error);
  }
);

export default instance; 